/**
 * 买手确认
 */
import * as React from 'react'
import { Card, Button, Form, Row, Col, Select } from 'antd'
import Authentication from '../../router/Authentication'
import { PageHeaderLayout, StandardTable } from '../../components'
import '../../assets/style/TableList.less'
const FormItem = Form.Item
const Option = Select.Option

export interface State {
  formValues: string
}

@Authentication
class BuyerConfirm extends React.Component<any, State> {
  
  state = {
    formValues: ''
  }
  componentDidMount () {
    this.search({}, {})
  }

  handleFormReset = () => {
    const { form } = this.props
    form.resetFields()
    this.setState({
      formValues: ''
    })
    this.search({}, {})
  }
  /**
   * 点击查询按钮
   */
  handleSearch = (e: any) => {
    e.preventDefault()
    const { form, data: { pagination } } = this.props
    form.validateFields((err: any, fieldsValue: any) => {
      if (err) return
      if(fieldsValue.suppliers) fieldsValue.suppliers = fieldsValue.suppliers.join(',')
      this.setState({
        formValues: fieldsValue
      })
      this.search({
        ...fieldsValue,
        pageIndex: pagination.current,
        pageSize: pagination.pageSize,
      }, pagination)
    })
  }
  /**
   * 调用查询方法
   */
  search (param: any, page: any) {
    const { list } = this.props
    list(param, page)
  }
  renderSimpleForm() {
    const { form, venders } = this.props
    const { getFieldDecorator } = form
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={7} sm={24}>
            <FormItem label="供应商">
              {getFieldDecorator('suppliers')(
                <Select placeholder="请输入/选择供应商" showSearch mode="multiple">
                  {venders.map( (vender: any, index: number) => (<Option key={index} value={vender.Key}>{vender.Value}</Option>))}
                </Select>)}
            </FormItem>
          </Col>
          <Col md={4} sm={24}>
            <span className="submitButtons">
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </span>
          </Col>
        </Row>
      </Form>
    )
  }
  /**
   * 筛选或者更改分页
   */
  handleStandardTableChange = (pagination: any, filtersArg: any, sorter: any) => {
    const { formValues } = this.state
    let params: any = formValues
    params = {
      ...params,
      pageIndex: pagination.current,
      pageSize: pagination.pageSize,
    }
    this.search(params, pagination)
  }
  //查看详情
  handleInfo = (no: string) => {
    this.props.history.push({ pathname: `/msqr/list/${no}` })
  }
  render () {
    const { loading, data } = this.props
    const columns: any [] = [
      {
        title: '供应商编号',
        dataIndex: 'supplierid',
      },
      {
        title: '供应商名称',
        dataIndex: 'suppliername',
      },
      {
        title: '上传图片数量',
        dataIndex: 'sendqty'
      },
      {
        title: '图片合格数量',
        dataIndex: 'passqty'
      },
      {
        title: '操作',
        render: (row: any) => (
          <React.Fragment>
            <a onClick={ () => this.handleInfo(row.id)}>买手确认</a>
          </React.Fragment>
        ),
      },
    ]
    return (
      <PageHeaderLayout
        title="">
        <Card bordered={false}>
          <div className="tableList">
            <div className="tableListForm">{this.renderSimpleForm()}</div>
            <StandardTable
              rowKey = "id"
              select = {false}
              loading={loading}
              data={data}
              columns={columns}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
      </PageHeaderLayout>
    )
  }
}

export default Form.create()(BuyerConfirm)